package com.topfun.dao;

import com.topfun.utils.JDBCUtil;
import com.topfun.vo.BetOrder;
import com.topfun.vo.SliderVo;
import com.topfun.vo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by huangda on 2017/8/10.
 * 用户dao
 */
public class UserDao {
    // ===========================================================
    // Constants
    // ===========================================================
    private static final Logger logger = LoggerFactory.getLogger(UserDao.class);

    private static final String SQL_QUERY_USER_BY_OPENID;
    private static final String SQL_QUERY_USER_BY_USERID;
    private static final String SQL_UPDATE_USER_TOKEN;
    private static final String SQL_INSERT_USER;
    private static final String SQL_UPDATE_USER_FAVORITED;
    private static final String SQL_UPDATE_USER_AMOUNT;
    private static final String SQL_UPDATE_USER_ROOKIEAWARD;
    private static final String SQL_UPDATE_USER_NOVICEBOOT;
    private static final String SQL_INSERT_BET_ORDER;
    private static final String SQL_QUERY_BET_ORDER_BY_ID;
    private static final String SQL_UPDATE_BET_ORDER_AWARD;
    private static final String SQL_QUERY_BET_PRIZE_SLIDER;

    static {
        SQL_QUERY_USER_BY_OPENID = "select userId,nickName,headImgUrl,sex,token,type,registerTime," +
                "openId,favorited,amount,rookieAward,noviceBoot from topfun.user where openId = ?";
        SQL_QUERY_USER_BY_USERID = "select userId,nickName,headImgUrl,sex,token,type,registerTime," +
                "openId,favorited,amount,rookieAward,noviceBoot from topfun.user where userId = ?";
        SQL_UPDATE_USER_TOKEN = "update topfun.user set token = ? where userId = ?";
        SQL_INSERT_USER = "insert into topfun.user(nickName,headImgUrl,sex,token,type,registerTime,openId,favorited,amount,rookieAward,noviceBoot)" +
                " values (?,?,?,?,?,?,?,?,?,?,?)";
        SQL_UPDATE_USER_FAVORITED = "update topfun.user set favorited = ? where userId = ?";
        SQL_UPDATE_USER_AMOUNT = "update topfun.user set amount =  ? where userId = ?";
        SQL_UPDATE_USER_ROOKIEAWARD = "update topfun.user set rookieAward = ? where userId = ?";
        SQL_UPDATE_USER_NOVICEBOOT = "update topfun.user set noviceBoot = ? where userId = ?";
        SQL_INSERT_BET_ORDER = "INSERT into topfun.`bet_order` (orderId,userId,amount,winningLevel,odds,bonus,award,result,createTime) values (?,?,?,?,?,?,?,?,?)";
        SQL_QUERY_BET_ORDER_BY_ID = "SELECT orderId,userId,amount,winningLevel,odds,bonus,award,result,createTime from topfun.bet_order where orderId = ?";
        SQL_UPDATE_BET_ORDER_AWARD = "update topfun.bet_order set award = ? where orderId = ?";
        SQL_QUERY_BET_PRIZE_SLIDER = "SELECT a.bonus,b.nickName from topfun.bet_order a LEFT JOIN topfun.`user` b on a.userId = b.userId where winningLevel > 0 ORDER BY createTime DESC LIMIT 10";
    }
    // ===========================================================
    // Fields
    // ===========================================================

    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter &amp; Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    // ===========================================================
    // Methods
    // ===========================================================
    public static User queryUserByOpenId(String pOpenId) {
        return JDBCUtil.queryForOne(User.class, SQL_QUERY_USER_BY_OPENID, new Object[]{pOpenId});
    }

    public static User queryUserByUserId(long pUserId) {
        return JDBCUtil.queryForOne(User.class, SQL_QUERY_USER_BY_USERID, new Object[]{pUserId});
    }
    public static int updateUserToken(long pUid, String pToken) {
        return JDBCUtil.update(SQL_UPDATE_USER_TOKEN, new Object[]{pToken, pUid});
    }

    public static long insertUser(User pUser) {
        long userId = JDBCUtil.insertForPrimaryKey(SQL_INSERT_USER, new Object[]{
                //nickName,headImgUrl,sex,token,
                pUser.nickName, pUser.headImgUrl, pUser.sex, pUser.token,
                //type,registerTime,openId,favorited
                pUser.type, pUser.registerTime, pUser.openId, pUser.favorited,
                //amount,rookieAward,noviceBoot
                pUser.amount,pUser.rookieAward,pUser.noviceBoot
        });
        pUser.userId = userId;
        return userId;
    }

    public static void updateUserFavorited(long userId, int favorited) {
        JDBCUtil.update(SQL_UPDATE_USER_FAVORITED, new Object[]{favorited,userId});
    }

    public static int updateUserAmount(long pUserId, int pAmount) {
        return JDBCUtil.update(SQL_UPDATE_USER_AMOUNT, new Object[]{pAmount,pUserId});
    }
    public static int updateUserRookieAward(long pUserId,int pRookieAward) {
        return JDBCUtil.update(SQL_UPDATE_USER_ROOKIEAWARD, new Object[]{pRookieAward,pUserId});
    }

    public static int updateUserNoviceBoot(long pUserId, int pNoviceBoot) {
        return JDBCUtil.update(SQL_UPDATE_USER_NOVICEBOOT, new Object[]{pNoviceBoot,pUserId});
    }

    public static BetOrder insertBetOrder(BetOrder betOrder) {
        long orderId = JDBCUtil.insertForPrimaryKey(SQL_INSERT_BET_ORDER,new Object[]{
                //orderId,userId,amount,winningLevel,
                betOrder.orderId,betOrder.userId,betOrder.amount,betOrder.winningLevel,
                //odds,bonus,award,result,createTime
                betOrder.odds,betOrder.bonus,betOrder.award,betOrder.result,betOrder.createTime
        });
        betOrder.orderId = orderId;
        return betOrder;
    }

    public static BetOrder queryBetOrderById(long orderId) {
        return JDBCUtil.queryForOne(BetOrder.class,SQL_QUERY_BET_ORDER_BY_ID,new Object[]{orderId});
    }

    public static int updateBetOrderAward(long orderId, int award) {
        return JDBCUtil.update(SQL_UPDATE_BET_ORDER_AWARD,new Object[]{award,orderId});
    }

    public static List<SliderVo> queryBetPrizeSlider() {
        return JDBCUtil.queryList(SliderVo.class,SQL_QUERY_BET_PRIZE_SLIDER,null);
    }
    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}
